{% load static %}

<!DOCTYPE html>
<html style="overflow-x:auto;overflow-y:auto;">
<head>
	<title>Menu Manage</title>
	
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="author" content="http://www.jeeplus.org/"/>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=9,IE=10" />
<meta http-equiv="Expires" content="0">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Cache-Control" content="no-store">
<script>var d = "";</script>
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
	
{% include 'base.html' %}
	
	<meta name="decorator" content="ani"/>
	

	
<script type="text/javascript">

var $treeTable=null;  
$(document).ready(function(){  
         
       $treeTable=$('#treeTable').treeTable({  
    	   theme:'vsStyle',	           
			expandLevel : 1,
			column:0,
			checkbox: false,
            url:'/system/menu/getChildren?parentId=',  
            callback:function(item) { 
				item.isShow = (item.isShow == '1'?true:false);
				var menuItemTpl= $("#menuItemTpl").html();
                var result = laytpl(menuItemTpl).render(item);
                return result;
            },  
            beforeClick: function($treeTable, id) { 
                //异步获取数据 这里模拟替换处理  
                $treeTable.refreshPoint(id);  
            },  
            beforeExpand : function($treeTable, id) {     
              
            },  
            beforeClose : function($treeTable, id) {    
            
            }  
        });  
       
       
});  
		  
	  
	  
function sort(id,isUpSort){  
		
		var $ctr=$("tr[id='"+id+"']",$treeTable);  
        var $trs=null;  
        if($ctr.attr("pId")!=null)  
           $trs=$("tr[pId='"+$ctr.attr("pId")+"']",$treeTable);  
        else  
           $trs=$("tr[depth='1']",$treeTable);   
        if($trs.size()>1) {  
             //递归深度  
            if(isUpSort) {  
               if($trs[0].id==$ctr[0].id){
            	   jp.error("It's the first one");
            	   return;
               }  
               var i=0;  
               $trs.each(function(k){  
                   if(this.id==$ctr[0].id) i=k;  
               });  
               var $ptr=$($trs[i-1]);  
                 
                 
               //封装  
               var indexB=0,indexE=0,depth=parseInt($ctr.attr("depth"),10);  
                 
               var $alltrs=$("tr",$treeTable);  
               for(var k=0;k<$alltrs.size();k++)  
               {  
                   if($alltrs[k].id==$ctr[0].id) {  
                       indexB=k;  
                   } else if(indexB>0&&parseInt($($alltrs[k]).attr("depth"),10)>depth) {  
                       indexE=k;  
                   } else if(indexB>0&&parseInt($($alltrs[k]).attr("depth"),10)<=depth) {  
                       break;  
                   }  
               }  
                 
               var selector="tr:eq("+indexB+")";  
               indexB++;  
               while(indexB<=indexE) {  
                   selector+=",tr:eq("+indexB+")";  
                   indexB++;  
               }  
               var moveNode = $(selector,$treeTable);
	  			   var targetNode = $ptr;
	  			   
	  			   var id1 = moveNode.attr("id");
	  			   var sort1 = parseInt(moveNode.attr("sort"));//交换排序值
	  			   var id2 = targetNode.attr("id");
  			   var sort2 = parseInt(targetNode.attr("sort"));
  			   if(sort1 > sort2){
  				   var c = sort1;
  				   sort1 = sort2;
  				   sort2 = c;
  			   }else if(sort1 == sort2){
  				   if(sort1 > 0){
  					 sort1 = sort1 -1;//向上移动，相等减1
  				   }else{
  					 sort2 = sort2 + 1;
  				   }
  				   
  			   }
               //交换  
                $.get("/system/menu/sort/?id1="+id1+"&sort1="+sort1+"&id2="+id2+"&sort2="+sort2, function(data){
	    	  		if(data.success){
	    	  			moveNode.attr("sort", sort1);
	    	  			targetNode.attr("sort", sort2);
	    	  			moveNode.insertBefore(targetNode);  
	    	  			jp.success(data.msg);
	    	  		}else{
	    	  			jp.error(data.msg);
	    	  		}
	           
	               
	   	   		})
              
            }else {  
               if($trs[$trs.size()-1].id==$ctr[0].id){
            	   jp.error("It's the last one"); 
            	   return;
            	}  
               var i=0;  
               $trs.each(function(k){  
                   if(this.id==$ctr[0].id) i=k;  
               });  
               var $ntr=$($trs[i+1]);  
             
               //封装  
               var indexB=0,indexE=0,depth=parseInt($ntr.attr("depth"),10);  
                 
               var $alltrs=$("tr",$treeTable);  
               for(var k=0;k<$alltrs.size();k++)  
               {  
                   if($alltrs[k].id==$ntr[0].id) {  
                       indexB=k;  
                   } else if(indexB>0&&parseInt($($alltrs[k]).attr("depth"),10)>depth) {  
                       indexE=k;  
                   } else if(indexB>0&&parseInt($($alltrs[k]).attr("depth"),10)<=depth) {  
                       break;  
                   }  
               }  
                 
               var selector="tr:eq("+indexB+")";  
               indexB++;  
               while(indexB<=indexE) {  
                   selector+=",tr:eq("+indexB+")";  
                   indexB++;  
               }  
               
               var moveNode = $(selector,$treeTable);
	  			   var targetNode = $ctr;
	  			   
	  			   var id1 = moveNode.attr("id");
  			   var sort1 = parseInt(targetNode.attr("sort"));//交换排序值
  			   var id2 = targetNode.attr("id");
  			   var sort2 = parseInt(moveNode.attr("sort"));
  			   
  			 	if(sort1 > sort2){
  				   var c = sort1;
  				   sort1 = sort2;
  				   sort2 = c;
  			   }else if(sort1 == sort2){
  				 sort2 = sort2 +1;//向下移动，相等加1
  			   }
  			   
             //交换  
              $.get("/system/menu/sort/?id1="+id1+"&sort1="+sort1+"&id2="+id2+"&sort2="+sort2, function(data){
	    	  		if(data.success){
	    	  			moveNode.attr("sort", sort1);
	    	  			targetNode.attr("sort", sort2);
	    	  			moveNode.insertBefore(targetNode);  
	    	  			jp.success(data.msg)
	    	  		}else{
	    	  			jp.error(data.msg);
	    	  		}
	           
	               
	   	   		})
               
            }   
              
        }  
        
	   	  
	}
	  
	
	function del(con,id){  
		 jp.confirm('确认要删除菜单吗？', function(){
			jp.loading();
    	  	$.get("/system/menu/del/?id="+id, function(data){
    	  		if(data.success){
    	  			$treeTable.del(id);
    	  			jp.success(data.msg);
    	  		}else{
    	  			jp.error(data.msg);
    	  		}
    	  	})
    	   
    	});

	} 
	 function generateSubMenu(con,id){
	 	 jp.confirm('Are you sure generate submenu?', function(){
	 		jp.loading();
	 	  	$.get("/system/menu/generateSubMenu/?id="+id, function(data){
	 	  		if(data.success){
					refreshNode(data);
	 	  			//$treeTable.del(id);
	 	  			jp.success(data.msg);
	 	  		}else{
	 	  			jp.error(data.msg);
	 	  		}
	 	  	})
	 	   
	 	});
	 
	 }
        
	function refreshMenu(){
		var index = jp.loading();
		$treeTable.refresh();
		jp.close(index);
	}

	function refreshNode(data) {//刷新节点
        var current_id = data.body.menu.id;
        var target = $treeTable.get(current_id);
        var old_parent_id = target.attr("pid") == undefined?'0':target.attr("pid");
        var current_parent_id = data.body.menu.parent_id;
        var current_parent_ids = data.body.menu.parent_ids;

        if(old_parent_id == current_parent_id){
            if(current_parent_id == '0'){
                $treeTable.refreshPoint(-1);
            }else{
                $treeTable.refreshPoint(current_parent_id);
            }
        }else{
            $treeTable.del(current_id);//刷新删除旧节点
            $treeTable.initParents(current_parent_ids, "0");
        }
	}
	
	function setDataRule(id, name) {
		$("#left").attr("class", "col-sm-6");
		setTimeout(function(){
			$("#right").fadeIn(500);
		},500)
		$("#menuLabel").html(name);
		$("#menuId").val(id);
		$('#dataRuleTable').bootstrapTable("refresh",{query:{menuId:id}})
    }

</script>
<script type="text/html" id="menuItemTpl">
	<td> <a  href="#" onclick="jp.openViewDialog('查看菜单', '/system/menu/view/?id={% verbatim %}{{d.id}}{% endverbatim %}','800px', '510px')"> 
	<i class="{% verbatim %}{{d.icon}}{% endverbatim %}"></i> {% verbatim %}{{d.name}}{% endverbatim %}</a></td>
	<td title="{% verbatim %}{{d.href}}{% endverbatim %}">{% verbatim %}{{d.href}}{% endverbatim %}</td>
	<td>
		<a href="javascript:;" onclick="sort('{% verbatim %}{{d.id}}{% endverbatim %}',true);"><i class="fa  fa-long-arrow-up"></i></a>
        <a href="javascript:;" onclick="sort('{% verbatim %}{{d.id}}{% endverbatim %}',false);"><i class="fa  fa-long-arrow-down"></i></a>
	</td>
	<td>
		{% verbatim %}{{# if(d.isShow){ }}<i class="fa fa-circle text-success ml5"></i>{{# }else{ }}
		<i class="fa fa-circle text-muted ml5"></i>{{# } }}{% endverbatim %}
	</td>
	<td>
		<div class="btn-group">
		 <button type="button" class="btn  btn-primary btn-xs dropdown-toggle" data-toggle="dropdown">
			<i class="fa fa-cog"></i>
			<span class="fa fa-chevron-down"></span>
		</button>
		<ul class="dropdown-menu" role="menu">
			
				<li><a href="#" onclick="jp.openViewDialog('View', '/system/menu/view/?id={% verbatim %}{{d.id}}{% endverbatim %}','800px', '510px')"><i class="fa fa-search-plus"></i> View</a></li>
			
			
				<li><a href="#" onclick="jp.openSaveDialog('Edit', '/system/menu/edit/?id={% verbatim %}{{d.id}}{% endverbatim %}','800px', '510px')"><i class="fa fa-edit"></i> Edit</a></li>
			
			
				<li><a  onclick="return del(this, '{% verbatim %}{{d.id}}{% endverbatim %}')"><i class="fa fa-trash"></i> Delete</a></li>
			
				<li><a  onclick="return generateSubMenu(this, '{% verbatim %}{{d.id}}{% endverbatim %}')"><i class="fa fa-plus-square-o "></i> Generate Sub Menu</a></li>
							
				<li><a href="#" onclick="jp.openSaveDialog('Add Sub Menu', '/system/menu/add/?parent_id={% verbatim %}{{d.id}}{% endverbatim %}','800px', '510px')"><i class="fa fa-plus"></i> Add Sub Menu</a></li>
			
		</ul>
		</div>
		
			 
		
  	</td>
</script>
	
<script>
$(document).ready(function() {
	$('#dataRuleTable').bootstrapTable({
		 
		  //请求方法
				method: 'post',
				//类型json
				dataType: "json",
				contentType: "application/x-www-form-urlencoded",
               //显示刷新按钮
               showRefresh: true,
               //显示切换手机试图按钮
               showToggle: true,
    	       showPaginationSwitch: true,
    	       //最低显示2行
    	       minimumCountColumns: 2,
               //是否显示行间隔色
               striped: true,
               //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）     
               cache: false,    
               //是否显示分页（*）  
               pagination: true,   
                //排序方式 
               sortOrder: "asc",  
               //初始化加载第一页，默认第一页
               pageNumber:1,   
               //每页的记录行数（*）   
               pageSize: 10,  
               //可供选择的每页的行数（*）    
               pageList: [10, 25, 50, 100],
               //这个接口需要处理bootstrap table传递的固定参数,并返回特定格式的json数据  
               url: "/ani/a/sys/dataRule/data",
               //默认值为 'limit',传给服务端的参数为：limit, offset, search, sort, order Else
               //queryParamsType:'',   
               ////查询参数,每次调用是会带上这个参数，可自定义                         
               queryParams : function(params) {
               	var searchParam = {};
               	searchParam.pageNo = params.limit === undefined? "1" :params.offset/params.limit+1;
               	searchParam.pageSize = params.limit === undefined? -1 : params.limit;
               	searchParam.orderBy = params.sort === undefined? "" : params.sort+ " "+  params.order;
               	searchParam.menuId = $("#menuId").val();
                   return searchParam;
               },
               //分页方式：client客户端分页，server服务端分页（*）
               sidePagination: "server",
               contextMenuTrigger:"right",//pc端 按右键弹出菜单
               contextMenuTriggerMobile:"press",//手机端 弹出菜单，click：单击， press：长按。
               contextMenu: '#context-menu',
               onContextMenuItem: function(row, $el){
                   if($el.data("item") == "edit"){
                       jp.go("/ani/a/sys/dataRule/form?id=" + row.id);
                   } else if($el.data("item") == "delete"){
                        jp.confirm('确认要删除该数据权限记录吗？', function(){
                       	jp.loading();
                       	jp.get("/ani/a/sys/dataRule/delete?id="+row.id, function(data){
                   	  		if(data.success){
                   	  			$('#dataRuleTable').bootstrapTable('refresh');
                   	  			jp.success(data.msg);
                   	  		}else{
                   	  			jp.error(data.msg);
                   	  		}
                   	  	})
                   	   
                   	});
                      
                   } 
               },
              
               onClickRow: function(row, $el){
               },
               columns: [{
		        checkbox: true
		       
		    }
			,{
		        field: 'name',
		        title: '数据规则名称',
		        sortable: true
		        ,formatter:function(value, row , index){
		        	return "<a href='/ani/a/sys/dataRule/form?id="+row.id+"'>"+value+"</a>";
		         }
		       
		    },{
		        field: 'className',
		        title: '实体类',
		        sortable: true
		       
		    }
			,{
		        field: 'field',
		        title: '字段',
		        sortable: true
		       
		    }
			,{
		        field: 'express',
		        title: '条件',
		        sortable: true,
		        formatter:function(value, row , index){
		        	return jp.getDictLabel([{"id":"b2b84bd17d8b4dafb0dd83c14c3c962d","createBy":{"id":"1","loginFlag":"1","roleNames":"","admin":true},"createDate":"2017-03-24 23:37:21","updateBy":{"id":"1","loginFlag":"1","roleNames":"","admin":true},"updateDate":"2017-03-24 23:37:21","label":"大于","value":"&gt;","sort":"1","dictType":{"id":"ebdd474a9cab463db594c7bc325c7ff4"}},{"id":"35ee2efb9daa45179328fe49542d8e64","createBy":{"id":"1","loginFlag":"1","roleNames":"","admin":true},"createDate":"2017-04-01 23:24:37","updateBy":{"id":"1","loginFlag":"1","roleNames":"","admin":true},"updateDate":"2017-04-01 23:24:37","label":"大于等于","value":"&gt;=","sort":"2","dictType":{"id":"ebdd474a9cab463db594c7bc325c7ff4"}},{"id":"9de5be7eea4f492fb726a958cca1d4b0","createBy":{"id":"1","loginFlag":"1","roleNames":"","admin":true},"createDate":"2017-04-01 23:25:16","updateBy":{"id":"1","loginFlag":"1","roleNames":"","admin":true},"updateDate":"2017-04-01 23:25:16","label":"小于","value":"&lt;","sort":"3","dictType":{"id":"ebdd474a9cab463db594c7bc325c7ff4"}},{"id":"a4de7bc6a5b94d54bac6c577b76e27a2","createBy":{"id":"1","loginFlag":"1","roleNames":"","admin":true},"createDate":"2017-04-01 23:25:31","updateBy":{"id":"1","loginFlag":"1","roleNames":"","admin":true},"updateDate":"2017-04-01 23:25:31","label":"小于等于","value":"&lt;=","sort":"4","dictType":{"id":"ebdd474a9cab463db594c7bc325c7ff4"}},{"id":"f7556a086d264cd29e0ceb417535fe61","createBy":{"id":"1","loginFlag":"1","roleNames":"","admin":true},"createDate":"2017-04-01 23:25:58","updateBy":{"id":"1","loginFlag":"1","roleNames":"","admin":true},"updateDate":"2017-04-01 23:25:58","label":"不等于","value":"&lt;&gt;","sort":"5","dictType":{"id":"ebdd474a9cab463db594c7bc325c7ff4"}},{"id":"39c83ee547644d7ca16e2ba1b1cf0ab7","createBy":{"id":"1","loginFlag":"1","roleNames":"","admin":true},"createDate":"2017-04-01 23:26:16","updateBy":{"id":"1","loginFlag":"1","roleNames":"","admin":true},"updateDate":"2017-04-01 23:26:16","label":"等于","value":"=","sort":"6","dictType":{"id":"ebdd474a9cab463db594c7bc325c7ff4"}},{"id":"f7c2d3753c77489e99c5e9a59e7ebd3d","createBy":{"id":"1","loginFlag":"1","roleNames":"","admin":true},"createDate":"2017-04-01 23:26:28","updateBy":{"id":"1","loginFlag":"1","roleNames":"","admin":true},"updateDate":"2017-04-01 23:26:28","label":"包含","value":"contain","sort":"7","dictType":{"id":"ebdd474a9cab463db594c7bc325c7ff4"}},{"id":"8a0dca98a94d46a486a796f4288a9863","createBy":{"id":"1","loginFlag":"1","roleNames":"","admin":true},"createDate":"2017-04-01 23:26:53","updateBy":{"id":"1","loginFlag":"1","roleNames":"","admin":true},"updateDate":"2017-04-01 23:26:53","label":"模糊匹配","value":"like","sort":"8","dictType":{"id":"ebdd474a9cab463db594c7bc325c7ff4"}}], value, "-");
		        }
		       
		    }
			,{
		        field: 'value',
		        title: '规则值',
		        sortable: true
		       
		    }
			,{
		        field: 'sqlSegment',
		        title: '自定义sql',
		        sortable: true
		       
		    }
			,{
		        field: 'remarks',
		        title: '备注信息',
		        sortable: true
		       
		    }, {
                field: 'operate',
                title: '操作',
                align: 'center',
                events: {
    		        'click .del': function (e, value, row, index) {

    		             jp.confirm('确认要删除该数据权限记录吗？', function(){
    		            	jp.loading();
    		            	jp.get("/ani/a/sys/dataRule/delete?id="+row.id, function(data){
    		        	  		if(data.success){
    		        	  			$('#dataRuleTable').bootstrapTable('refresh');
    		        	  			jp.success(data.msg);
    		        	  		}else{
    		        	  			jp.error(data.msg);
    		        	  		}
    		        	  	})
    		        	   
    		        	});
    		           
    		        
    		        	
    		        },
    		        
    		        'click .edit': function (e, value, row, index) {

    		        	jp.openSaveDialog('编辑数据规则', '/ani/a/sys/dataRule/form?id='+row.id+'&menuId='+$("#menuId").val(),'800px', '580px');
    		        }
    		    },
                formatter:  function operateFormatter(value, row, index) {
    		        return [
						'<a href="#" class="edit" title="编辑" >[编辑] </a>',
						'<a href="#" class="del" title="删除" >[删除] </a>'
    		        ].join('');
    		    }
            }
		     ]

		});
		
	
	
	  if(navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i)){//如果是移动端

		 
		  $('#dataRuleTable').bootstrapTable("toggleView");
		}
	  
	  $('#dataRuleTable').on('check.bs.table uncheck.bs.table load-success.bs.table ' +
                'check-all.bs.table uncheck-all.bs.table', function () {
            $('#remove').prop('disabled', ! $('#dataRuleTable').bootstrapTable('getSelections').length);
            $('#edit').prop('disabled', $('#dataRuleTable').bootstrapTable('getSelections').length!=1);
        });
		  
		    
		$("#dataRuleAddButton").click(function(){
			
			jp.openSaveDialog('添加数据规则', '/ani/a/sys/dataRule/form?menuId='+$("#menuId").val(),'800px', '580px');
			
		});
		
		
	});
		
  function getIdSelections() {
        return $.map($("#dataRuleTable").bootstrapTable('getSelections'), function (row) {
            return row.id
        });
    }
  
  function deleteAll(){

		jp.confirm('确认要删除该数据权限记录吗？', function(){
			jp.loading();  	
			jp.get("/ani/a/sys/dataRule/deleteAll?ids=" + getIdSelections(), function(data){
         	  		if(data.success){
         	  			$('#dataRuleTable').bootstrapTable('refresh');
         	  			jp.success(data.msg);
         	  		}else{
         	  			jp.error(data.msg);
         	  		}
         	  	})
          	   
		})
  }
  function edit(){
      jp.go("/ani/a/sys/dataRule/form?id=" + getIdSelections());
  }

  function refresh() {
      $('#dataRuleTable').bootstrapTable('refresh');
  }
  
</script>

</head>
<body id="" class=""  style="">
	
	<div class="wrapper wrapper-content">
		<div class="panel panel-primary">
			<div class="panel-heading">
				<h3 class="panel-title">菜单列表	  </h3>
			</div>
			<div class="panel-body">
			
			 <div class="row">
				<div id="left" class="col-sm-12">
				<!-- 工具栏 -->
			    <div id="toolbar" class="treetable-bar">
			    	
						<a id="add" class="btn btn-primary" onclick="jp.openSaveDialog('Add Menu', '/system/menu/add/','800px', '500px')"><i class="glyphicon glyphicon-plus"></i> 新建菜单</a><!-- 增加按钮 -->
					
					<button class="btn btn-default" data-toggle="tooltip" data-placement="left" onclick="refreshMenu()" title="刷新"><i class="glyphicon glyphicon-repeat"></i> 刷新</button>
			    </div><!-- 工具栏结束 -->
	
				<table id="treeTable" class="table table-hover">
					<thead>
						<tr>
							<th>名称</th>
							<th>链接</th>
							<th>排序</th>
							<th>显示/隐藏</th>
							<th>操作</th>
						</tr>
					</thead>
				    <tbody>  
				    </tbody>  
				</table> 
	 		</div>
	 		
	 		 
		    <div  id="right" class="panel panel-default col-sm-6" style="display:none">
				<div class="panel-heading">
					<h3 class="panel-title"><label>数据规则列表，所属菜单: </label><font id="menuLabel"></font><input type="hidden" id="menuId"/></h3>
				</div>
				<div class="panel-body">
				 <div id="dataRuleToolbar">
					<button id="dataRuleAddButton"  class="btn btn-outline btn-primary" title="添加规则"><i class="fa fa-plus"></i> 添加规则</button>
				</div>
				 <!-- 表格 -->
			    <table id="dataRuleTable"
			           data-toolbar="#dataRuleToolbar"
			           data-id-field="id">
			    </table>
			   </div>
			</div>
			</div>
		   </div>
		</div>
	</div>

</body>
</html>